<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>新增员工</title>
  <style>
    body { font-family: Arial, sans-serif; }
    .container { max-width: 800px; margin: 0 auto; }
    .header { background-color: #4CAF50; color: white; padding: 15px; }
    .nav { overflow: hidden; background-color: #333; }
    .nav a { float: left; display: block; color: white; text-align: center; padding: 14px 16px; text-decoration: none; }
    .nav a:hover { background-color: #ddd; color: black; }
    .content { padding: 20px; }
    .form-group { margin-bottom: 15px; }
    .form-group label { display: block; margin-bottom: 5px; }
    .form-group input, .form-group select { width: 100%; padding: 8px; }
    .btn { background-color: #4CAF50; color: white; padding: 10px 15px; border: none; cursor: pointer; }
    .btn:hover { background-color: #45a049; }
    .back-btn { background-color: #f44336; color: white; padding: 10px 15px; border: none; cursor: pointer; text-decoration: none; }
    .back-btn:hover { background-color: #d32f2f; }
  </style>
</head>
<body>
<div class="container">
  <div class="header">
    <h1>新增员工</h1>
  </div>

  <div class="nav">
    <a href="index.jsp">首页</a>
    <a href="employeeList.jsp">员工管理</a>
    <a href="departmentList.jsp">部门管理</a>
    <a href="salaryList.jsp">工资管理</a>
    <a href="#">系统设置</a>
  </div>

  <div class="content">
    <form action="saveEmployee.jsp" method="post">
      <div class="form-group">
        <label>姓名：</label>
        <input type="text" name="name" required>
      </div>
      <div class="form-group">
        <label>性别：</label>
        <select name="gender">
          <option value="男">男</option>
          <option value="女">女</option>
        </select>
      </div>
      <div class="form-group">
        <label>年龄：</label>
        <input type="number" name="age" required>
      </div>
      <div class="form-group">
        <label>部门：</label>
        <select name="department_id">
          <%
            Connection conn = null;
            Statement stmt = null;
            ResultSet rs = null;

            try {
              Class.forName("com.mysql.jdbc.Driver");
              conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/company", "root", "password");
              stmt = conn.createStatement();
              String sql = "SELECT id, name FROM department";
              rs = stmt.executeQuery(sql);

              while (rs.next()) {
          %>
          <option value="<%= rs.getInt("department_id") %>"><%= rs.getString("name") %></option>
          <%
              }
            } catch (Exception e) {
              e.printStackTrace();
            } finally {
              try {
                if (rs != null) rs.close();
                if (stmt != null) stmt.close();
                if (conn != null) conn.close();
              } catch (SQLException se) {
                se.printStackTrace();
              }
            }
          %>
        </select>
      </div>
      <div class="form-group">
        <label>职位：</label>
        <input type="text" name="position" required>
      </div>
      <div class="form-group">
        <label>入职日期：</label>
        <input type="date" name="hire_date" required>
      </div>
      <button type="submit" class="btn">保存</button>
      <a href="employeeList.jsp" class="back-btn">返回</a>
    </form>
  </div>
</div>
</body>
</html>
